package converts

import (
	"encoding/json"
	"gitee.com/captials-team/ubdframe/src/domain/dto/iot_aliyun"
	"gitee.com/captials-team/ubdframe/src/domain/dto/iot_hmbirdthing"
)

func AliYunIotToHmBirdIot(d interface{}) string {
	if tmp, ok := d.(iot_aliyun.ThingDataType); ok {
		s, _ := json.Marshal(tmp.Specs)
		s, _ = json.Marshal(map[string]string{
			"type":  tmp.Type,
			"specs": string(s),
		})
		return string(s)
	}

	if tmp, ok := d.(iot_hmbirdthing.ThingDataTypeSpec); ok {
		s, _ := json.Marshal(tmp.Specs)
		s, _ = json.Marshal(map[string]string{
			"type":  tmp.Type,
			"specs": string(s),
		})
		return string(s)
	}

	if tmp, ok := d.([]iot_hmbirdthing.ThingDataItem); ok {
		var arr []interface{}
		for _, v := range tmp {
			arr = append(arr, map[string]interface{}{
				"code": v.Code,
				"name": v.Name,
				"type_spec": map[string]string{
					"type":  v.TypeSpec.Type,
					"specs": AliYunIotToHmBirdIot(v.TypeSpec.Specs),
				},
			})
		}
		s, _ := json.Marshal(arr)
		return string(s)
	}

	s, _ := json.Marshal(d)
	return string(s)
}
